﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stock.Dao;
using Stock.Model;
using Stock.Model.DB;
using Stock.Model.Tencent;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Stock.Client.Utils
{
    public class DateUtils
    {
        /// <summary>
        /// 调用工作日API
        /// </summary>
        public static string GetHolidays(string year, string month)
        {
            //string url = string.Format(ConfigurationManager.AppSettings["HOLIDAY"].ToString(), DateTime.Now.ToString("yyyyMM"));
            string url = string.Format(ConfigurationManager.AppSettings["HOLIDAY"].ToString(), year + (month.Length == 1 ? "0" + month : month));

            //Dictionary<string, dynamic> mode = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(HttpUtils.GetSendAsync("", url, "").Result);
            //WebClient webClient = new WebClient();

            //var postvars = new NameValueCollection { { "d", year + month }, { "back", "json" }, { "info", "2" } };
            //var byteresult = await webClient.UploadValuesTaskAsync(url, "POST", postvars);//请求地址,传参方式,参数集合
            string result = HttpUtils.GetSendAsync(url, "").Result; //获取返回值
            JObject keyValuePairs = (JObject)JsonConvert.DeserializeObject(result);

            if (keyValuePairs.Count == 0)
            {
                return "没有获取到日期数据";
            }
            //转换
            Holidays holidays = new Holidays() { holidayLists = new List<HolidayList>() };
            holidays.year = year;
            holidays.month = month;
            int days = DateTime.DaysInMonth(Convert.ToInt32(year), Convert.ToInt32(month));
            for (int i = 1; i <= days; i++)
            {
                holidays.holidayLists.Add(new HolidayList()
                {
                    date = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), i),
                    status = Convert.ToInt32(keyValuePairs[year + (month.Length == 1 ? "0" + month : month)][(month.Length == 1 ? "0" + month : month) + i.ToString("00")])
                });
            }

            //保存
            return PublicDao.InsertDate(holidays);
        }

        /// <summary>
        /// 获取历史价格
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="code"></param>
        /// <param name="dateLevel"></param>
        /// <param name="fq"></param>
        /// <returns></returns>
        public static string GetStock_History_Tencent(DateTime start, DateTime end, string code, string dateLevel = "day", string fq = "qfq")
        {

            // https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param=sh600519,day,2020-3-1,2021-3-1,500,qfq
            //https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param={0},{1},{2},{3},{4},{5}
            string url = string.Format(ConfigurationManager.AppSettings["TENCENT_HISTORY"].ToString(), PublicUtils.GetPrefixCode(code), dateLevel,
                  start.Year + "-" + start.Month + "-" + start.Day, end.Year + "-" + end.Month + "-" + end.Day, end.Subtract(start).Days, fq);

            string getValue = HttpUtils.GetSendAsync(url, "").Result;
            return getValue;
        }

        /// <summary>
        /// 获取简易信息
        /// </summary>
        /// <param name="codes"></param>
        /// <returns></returns>
        public static string GetStockSingleInfo(string codes)
        {
            //http://qt.gtimg.cn/q=s_sh600519,s_sh600518
            //v_s_sh600519 = "1~贵州茅台~600519~2111.40~63.40~3.10~31549~652978~~26523.36~GP-A"; v_s_sh600518 = "1~*ST康美~600518~3.01~-0.16~-5.05~156946~4724~~417.30~GP-A";
            string url = ConfigurationManager.AppSettings["TENCENT"].ToString();
            string[] paramCode = codes.Split(',');

            for (int i = 0; i < paramCode.Length; i++)
                url += "s_" + PublicUtils.GetPrefixCode(paramCode[i]) + ",";

            string getValue = HttpUtils.GetSendAsync(url, "").Result;

            return getValue;
        }
    }
}
